<template>

  <div class="app">
    <h1>欢迎使用泊客湾酒店后台管理系统</h1>

    <el-card style="width: 600px;height: 300px;margin: 230px auto;
    background-color: rgba(255,255,255,0.3)">

      <!--label-width设置文本所占的宽度如果不设置,文本单独显示一行-->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="60px" style="width: 400px;margin: 50px auto;">
        <el-form-item label="账户" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <!--让某一个元素位置进行微调时 使用相对定位-->
          <el-button type="primary" @click="login('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>


</template>

<script>
export default {

  data: function() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    }

  },
  name: "LoginView",
  methods:{
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9085/admins/login';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '登录成功！',
                type: 'success'
              });
              let jwt = responseBody.data;
              console.log('jwt = ' + jwt);
              localStorage.setItem('jwt', jwt);
              console.log('已经将JWT存入到localStorage中，即将跳转页面……');
              this.$router.push('/');
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }

}
</script>

<style scoped>
.app{
/*  background-image: url(../img/3.jpg); */
  background-image: url(https://img2.baidu.com/it/u=3143684719,1448494249&fm=253&fmt=auto&app=138&f=PNG?w=1113&h=500);
  margin: 0;  /*去掉自带的外边距*/
  /*cover封面  把背景图片作为封面*/
  background-size: cover;
  /*禁止重复*/
  background-repeat: no-repeat;
  text-align: center;
  overflow: hidden;
  height: 930px;
}

h1{
  font-size: 30px;
  color: #0096dc;
  margin-top: 50px;
  margin-bottom: 0;
}

h2{
  font-size: 20px;color: #0096dc;
  margin-top:50px;
  margin-bottom: 0;
}

</style>
